import java.util.Arrays;

public class Solution152 {
    public int maxProduct(int[] nums) {
        int[] f=new int[nums.length];
        int[] g=new int[nums.length];
        if (nums.length==1){
            return nums[0];
        }
        if (nums[0]>=0){
            f[0]=nums[0];
        }else{
            g[0]=nums[0];
        }
        for (int i=1;i<nums.length;i++){
            if (nums[i]>=0){
                f[i]=Math.max(nums[i],f[i-1]*nums[i]);
                g[i]=g[i-1]*nums[i];
            }else{
                g[i]=Math.min(nums[i],f[i-1]*nums[i]);
                f[i]=g[i-1]*nums[i];
            }
        }
        return Arrays.stream(f).max().getAsInt();
    }

    public static void main(String[] args) {
        System.out.println(new Solution152().maxProduct(new int[]{2,3,-2,4}));
    }
}
